package logic

import (
	"context"
	"demo/internal/errorx"
	"demo/internal/svc"
	"demo/internal/types"
	"demo/model"
	"fmt"
	"strconv"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/logx"
)


type ServeyInfoByClfLogic struct{
	logx.Logger
	ctx context.Context
	svcCtx *svc.ServiceContext
	modelS model.ServeyModel
}

func NewServeyInfoByClfLogic(ctx context.Context,svcCtx *svc.ServiceContext)*ServeyInfoByClfLogic{
	return &ServeyInfoByClfLogic{
		Logger: logx.WithContext(ctx),
		ctx: ctx,
		svcCtx: svcCtx,
		modelS: model.NewServeyModel(svcCtx.Orm,svcCtx.Config.Cache),
	}
}

func (l *ServeyInfoByClfLogic)InfoByClfId(req *types.ServeyInfoClassifyRequest)(resp *types.ServeyInfoResponse,err error){
	// 直接查询 servey
	query := l.modelS.SelectBuilder()
	start := (req.Page - 1)*req.PageSize
	query = query.InnerJoin("sev_sev_clf as ssc on ssc.sev_clf_id = $1 and ssc.sev_id = servey.id",req.ServeyClassifyId).
			Where(squirrel.Eq{"is_deleted":no_delete}).Limit(uint64(req.PageSize)).Offset(uint64(start))
	sqlData,err := l.modelS.GetListByPage(l.ctx,nil,query)
	if err != nil {
		return nil,errorx.NewBizError(500,"SQL查询错误")
	}
	fmt.Println("获取到的sql数据时：",len(sqlData))
	data := make([]types.ServeyItemResponse,0)
	for _,val := range sqlData {
		item := &types.ServeyItemResponse{
			Id: val.Id,
			CreatorId: val.CreatorId,
			Title: val.Title,
			Description: val.Description,
			CreateTime: strconv.FormatInt(val.CreateTime,10),
			PublishTime: strconv.FormatInt(val.PublishTime,10),
			EndTime: strconv.FormatInt(val.EndTime,10),
			IsPublic: val.IsPublic,
			IsEnd: val.IsEnd,
			VisitNum: val.VisitNum,
		}
		data = append(data, *item)
	}
	
	return &types.ServeyInfoResponse{
		Code: 200,
		Msg: "查询成功",
		Data: data,
	},nil
}